package com.demo.controller;

import com.demo.controller.base.BaseCtrl;
import com.demo.controller.model.RSResult;
import com.demo.po.LogPo;
import com.demo.service.ILogService;
import com.demo.service.common.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/log")
public class LogController extends BaseCtrl {

    private static final Logger logger = LoggerFactory.getLogger(LogController.class);

    private final ILogService logService;

    @Autowired
    public LogController(ILogService logService) {
        this.logService = logService;
    }

    @PostMapping(value = "/list")
    @ResponseBody
    public RSResult list(@RequestBody LogPo log) {
        logger.info("Start get list of the table : log, the request is : {}", log);
        RSResult result = new RSResult();
        try {
            int count = logService.countLogs(log);
            List<LogPo> list = new ArrayList<>();
            if (count > 0) {
                log.setPagestart((log.getCurpage() - 1) * log.getPagesize());
                list = logService.findLogsByPage(log);
            }
            result.setResult(0);
            result.setTotal(count);
            result.setData(list);
        } catch (BizException ex) {
            logger.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        logger.info("Get list of the table : log, success.");
        return result;
    }

    @PostMapping(value = "/detail")
    @ResponseBody
    public RSResult info(@RequestBody LogPo log) {
        logger.info("Get detail object of the table : log, the request is : {}", log);
        RSResult result = new RSResult();
        try {
            LogPo obj = logService.getLog(log.getId());
            result.setResult(0);
            result.setData(obj);
        } catch (BizException ex) {
            logger.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        logger.info("Get detail object of the table : log, success.");
        return result;
    }

    @PostMapping(value = "/new")
    @ResponseBody
    public RSResult add(@RequestBody LogPo log) {
        logger.info("Add object to the table : log, the request is : {}", log);
        RSResult result = new RSResult();
        try {
            Integer id = logService.addLog(log);
            result.setResult(0);
            result.setData(id);
        } catch (BizException ex) {
            logger.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        logger.info("Add object to the table : log, success.");
        return result;
    }

    @PostMapping(value = "/update")
    @ResponseBody
    public RSResult update(@RequestBody LogPo log) {
        logger.info("Update object from the table : log, the request is : {}", log);
        RSResult result = new RSResult();
        try {
            logService.updateLog(log);
            result.setResult(0);
        } catch (BizException ex) {
            logger.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        logger.info("Update object from the table : log, success.");
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public RSResult delete(@RequestBody LogPo log) {
        logger.info("Delete object from the table : log, the request is : {}", log);
        RSResult result = new RSResult();
        try {
            logService.deleteLog(log.getId());
            result.setResult(0);
        } catch (BizException ex) {
            logger.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        logger.info("Delete object from the table : log, success.");
        return result;
    }
}
